<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Services\Distribution;
use App\Admin\Models\distributionLevel;
use App\Admin\Models\Common;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use DB;

class DistributionLevelController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分销等级')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('分销等级')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('分销等级')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('分销等级')
            ->description('新增')
            ->body($this->form());
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributionLevel);
        $grid->dlId('ID');
        $grid->dlName('等级名称');
        $grid->dlMemo('等级备注');
        $grid->createTime('上传时间');

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});
		
		$grid->actions(function ($actions) {
//		    $actions->disableDelete();
//		    $actions->disableEdit();
//		    $actions->disableView();
		});

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$DistributionPrecept = new DistributionLevel();
        $show = new Show($DistributionPrecept->findOrFail($id));

        $show->dlId('ID');
        $show->dlName('等级名称');
        $show->dlMemo('等级备注');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(DistributionLevel::class, function (Form $form) {
			
			$form->hidden('dlId');
			$form->text('dlName', '等级名称');
			$form->text('dlMemo', '等级备注');
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
//			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
//			    $footer->disableCreatingCheck();
			
			});
        });
    }
    
    
    public function getDisCountList(Request $request, Content $content){
    	
    	
    	
    	$sId = Admin::user()->sId;
    	$auCode = $request->auCode;
    	
    	if (!Admin::user()->inRoles(['manager'])){
    		if (Admin::user()->auCode != $auCode){
	    		return "无权限";
	    	}
    	}
    	
    	$auInfo = DB::table('admin_users')->where('sId', $sId)->where('auCode', $auCode)->first();
    	
    	$headers1 = ['姓名', '下级总代数', '下级高级会员数', '下级天使会员数', '下级零售数'];
    	$headers2 = ['姓名', '下下级总代数', '下下级高级会员数', '下下级天使会员数', '下下级零售数'];
    	$levelArray = ['总代', '高级会员', '天使会员', '零售'];
    	$Distribution = new Distribution();
    	$lowerList = $Distribution->getLowerLevelCount($sId, $auCode);
    	$underTheLowerList = $Distribution->getUnderTheLowerLevelCount($sId, $auCode);
    	$showLowerTable = [$auInfo->name, 0, 0, 0, 0];
    	$showUnderTheLowerTable = [$auInfo->name, 0, 0, 0, 0];
    	foreach($lowerList as $lowerInfo){
    		for($i=0;$i<count($levelArray);$i++){
    			if ($lowerInfo->alName == $levelArray[$i]){
    				$showLowerTable[$i+1] = $lowerInfo->num;
    			}
    		}
    	}
    	foreach($underTheLowerList as $underTheLowerInfo){
    		for($i=0;$i<count($levelArray);$i++){
    			if ($underTheLowerInfo->alName == $levelArray[$i]){
    				$showUnderTheLowerTable[$i+1] = $underTheLowerInfo->num;
    			}
    		}
    	}
    	
    	
        $rows1 = array($showLowerTable);
        $rows2 = array($showUnderTheLowerTable);

        $table1 = new Table($headers1, $rows1);
        $table2 = new Table($headers2, $rows2);

        $content->row((new Box('下级数量', $table1))->style('primary')->solid());
    	$content->row((new Box('下下级数量', $table2))->style('warning')->solid());
    	
    	
    	$lowerTree = $Distribution->getLowerTree($sId, $auCode);
    	$treeHtml = '';

    	foreach($lowerTree as $lowerInfo){
    		$treeHtml = $treeHtml.'<div class="box-body">';
	    	$treeHtml = $treeHtml.'	<ul class="todo-list ui-sortable">';
	    	$treeHtml = $treeHtml.'		<li>';
	    	if ($lowerInfo[1] == 1){
	    		$treeHtml = $treeHtml.'			<span class="handle ui-sortable-handle">';
		    	$treeHtml = $treeHtml.'				<i class="fa fa-ellipsis-v"></i>';
		    	$treeHtml = $treeHtml.'				<i class="fa fa-ellipsis-v"></i>';
		    	$treeHtml = $treeHtml.'			</span>';
	    	} else if ($lowerInfo[1] == 2){
	    		$treeHtml = $treeHtml.'			<span class="handle ui-sortable-handle">';
		    	$treeHtml = $treeHtml.'				<i class="fa fa-ellipsis-v" style="margin-left: 50px;"></i>';
		    	$treeHtml = $treeHtml.'				<i class="fa fa-ellipsis-v" style=""></i>';
		    	$treeHtml = $treeHtml.'			</span>';
	    	}
	    	$treeHtml = $treeHtml.'			<span class="text">'.$lowerInfo[0]->userName.'</span>';
	    	$treeHtml = $treeHtml.'			<small class="label label-danger"><i class="fa fa-address-book"></i>&nbsp;&nbsp;'.$lowerInfo[0]->alName.'</small>';
	    	$treeHtml = $treeHtml.'			<div class="tools">';
//	    	$treeHtml = $treeHtml.'				<i class="fa fa-edit"></i>';
//	    	$treeHtml = $treeHtml.'				<i class="fa fa-trash-o"></i>';
	    	$treeHtml = $treeHtml.'			</div>';
	    	$treeHtml = $treeHtml.'		</li>';
	    	$treeHtml = $treeHtml.'	</ul>';
	    	$treeHtml = $treeHtml.'</div>';
    	}
    	
    	$content->row((new Box('下级树', $treeHtml))->style('success')->solid());
    	
    	//http://localhost:8084/admin/stats/getDisStatsList?userCode=c15189648af19bf30e893e87154c5a78
    	$buttonHtml = '<a href="'.route('getDisStatsList').'?userCode='.$auCode.'" type="button" class="btn bg-olive btn-flat margin">分销金额统计</a>';
    	$content->row($buttonHtml);
    	
    	$content->header('分销下线')->description('统计');
    	
        return $content;
    }
    
}
